<template>
  <div class="app-container">
    <div class="content-container">
      <div class="search-bar">
        <el-form ref="queryFormRef" :model="queryParams" :inline="true">
          <el-form-item prop="keywords" label="关键字">
            <el-input v-model="queryParams.keywords" placeholder="请输入关键字" clearable />
          </el-form-item>

          <el-form-item>
            <el-button type="primary" icon="search" @click="handleQuery">搜索</el-button>
            <el-button icon="refresh" @click="handleResetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <el-card shadow="never" class="table-card">
        <div class="table-card-body">
          <div class="mb-10px">
            <el-button type="success" icon="plus" @click="handleOpenDialog">新增</el-button>
          </div>

          <BasePageTable
            :table-data="tableData"
            :table-columns-data="tableColumnsData"
            :total="total"
            :loading="loading"
            :page-size="queryParams.pageSize"
            :page-num="queryParams.pageNum"
            @handleQuery="handlePageChange"
          >
            <template #gender="{ row, name }">
              {{ getLabelByValue(GenderEnum, row.gender) }}
            </template>
            <template #status="{ row, name }">
              <el-switch
                v-model="row.status"
                :active-value="1"
                :inactive-value="0"
                :title="getLabelByValue(UserStatusEnum, row.status)"
                @change="
                  ($event) => {
                    handleChange($event, row);
                  }
                "
              ></el-switch>
            </template>
            <template #avatar="{ row, name }">
              <el-avatar :size="50" :src="row.avatar" />
            </template>
            <template #operation="{ row, name }">
              <el-button link type="primary" icon="Edit" @click="handleEdit(row)">编辑</el-button>
              <el-button link type="danger" icon="Delete" @click="handleDelete(row)">
                删除
              </el-button>
            </template>
          </BasePageTable>
        </div>
      </el-card>
    </div>
    <user-form
      :id="currentRow.id"
      v-model="formVsisble"
      :data="currentRow"
      @load-data="handleQuery"
    ></user-form>
  </div>
</template>

<script setup lang="ts">
import BasePageTable from "@/components/BasePageTable/index.vue";
import UserAPI from "@/api/system/user";
import UserForm from "@/views/system/employee/components/user-form.vue";
import { Message } from "@element-plus/icons-vue";
import { getLabelByValue } from "@/utils/enums";
import { GenderEnum, UserStatusEnum } from "../../../enums/system/UserEnum";

const loading = ref(false);
const formVsisble = ref(false);
const total = ref(0);
const queryFormRef = ref();
const currentRow = ref({});

const queryParams = reactive({
  keywords: "",
  pageNum: 1,
  pageSize: 10,
});
const tableData = ref([]);
const tableColumnsData = ref([
  {
    prop: "username",
    label: "用户名",
    showOverflowTooltip: true,
  },
  {
    prop: "nickname",
    label: "用户昵称",
    showOverflowTooltip: true,
  },
  {
    prop: "mobile",
    label: "手机号",
    width: 200,
    showOverflowTooltip: true,
  },
  {
    prop: "gender",
    label: "性别",
    width: 200,
    showOverflowTooltip: true,
    customSlot: "gender", // 自定义插槽名称
  },
  {
    prop: "avatar",
    label: "头像",
    width: 150,
    showOverflowTooltip: true,
    customSlot: "avatar",
  },
  {
    prop: "roleNames",
    label: "角色名称",
    width: 200,
    showOverflowTooltip: true,
  },
  {
    prop: "status",
    label: "用户状态",
    width: 200,
    showOverflowTooltip: true,
    customSlot: "status",
  },
  {
    prop: "createTime",
    label: "创建时间",
    width: 150,
    showOverflowTooltip: true,
  },
  {
    prop: "operation",
    label: "操作",
    width: 200,
    align: "center",
    fixed: "right",
    showOverflowTooltip: true,
    customSlot: "operation", // 自定义插槽名称
  },
]);
// 查询
function handleQuery() {
  loading.value = true;
  UserAPI.getPage(queryParams)
    .then((data: any) => {
      tableData.value = data.data || [];
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}
const handlePageChange = (page: PageQuery) => {
  queryParams.pageNum = page.pageNum;
  queryParams.pageSize = page.pageSize;
  handleQuery();
};
// 重置查询
function handleResetQuery() {
  queryFormRef.value.resetFields();
  queryParams.pageNum = 1;
  handleQuery();
}
function handleOpenDialog() {
  formVsisble.value = true;
  currentRow.value = "";
}
function handleEdit(val) {
  currentRow.value = val;
  formVsisble.value = true;
}
async function handleDelete(val) {
  const res = await ElMessageBox.confirm("确认删除吗?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  });

  if (!res) return;
  loading.value = true;
  try {
    await UserAPI.delete(val.id);
    handleQuery();
    Message.success("删除成功");
  } catch (e) {
    Message.success("删除失败");
  } finally {
    loading.value = false;
  }
}

async function handleChange($event, row) {
  try {
    console.log("row", row);
    const params = {
      status: $event,
      userId: row.id,
    };
    await UserAPI.updateStatus(params);
    handleQuery();
  } catch (e) {
    console.log(e);
  }
}
onMounted(async () => {
  handleQuery();
});
</script>
